import 'package:demo/app/modules/main/home/home_view.dart';
import 'package:demo/app/modules/main/mine/mine_view.dart';
import 'package:demo/app/modules/main/msg/msg_view.dart';
import 'package:demo/app/modules/main/product/product_view.dart';
import 'package:demo/theme/app_colors.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';

import 'main_controller.dart';

class MainView extends GetView<MainController> {
  const MainView({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Obx(() => Scaffold(
        body: WillPopScope(
        onWillPop: () => clickBack(),
        child: getPages()[controller.currentIndex.value]),
      // body: getPages()[controller.currentIndex.value],
      bottomNavigationBar: BottomNavigationBar(
        selectedFontSize:12,
        fixedColor: Color_main, //选中的颜色
        currentIndex: controller.currentIndex.value, //步骤3新增：第几个菜单选中
        type: BottomNavigationBarType.fixed, //如果有4个或者4个以上的
        onTap: (index) => controller.setCurrentIndex(index),
        items: getBottom(),
      ),
    ));
  }

  List<BottomNavigationBarItem> getBottom(){
    List<BottomNavigationBarItem> list = [];
    list.add(BottomNavigationBarItem(
      icon: Image.asset(controller.currentIndex.value == 0 ? "assets/images/icon_home_p.png" : "assets/images/icon_home_n.png",width: 24.w,height: 24.w),
      label: "首页",
    ));
    if(controller.listSize == 4){
      list.add(BottomNavigationBarItem(
        icon: Image.asset(controller.currentIndex.value == 1 ? "assets/images/icon_loan_p.png" : "assets/images/icon_loan_n.png",width: 24.w,height: 24.w),
        label: "产品",
      ));
    }
    list.add(BottomNavigationBarItem(
      icon: Image.asset(controller.currentIndex.value == (controller.listSize == 4 ? 2 : 1) ? "assets/images/icon_msg_p.png" : "assets/images/icon_msg_n.png",width: 24.w,height: 24.w),
      label: "消息",
    ));
    list.add(BottomNavigationBarItem(
      icon: Image.asset(controller.currentIndex.value == (controller.listSize == 4 ? 3 : 2) ? "assets/images/icon_mine_p.png" : "assets/images/icon_mine_n.png",width: 24.w,height: 24.w),
      label: "我的",
    ));
    return list;
  }


  //步骤4新增：将页面放在List里，切换页面就是用下标选中当前页面切换，注意，每一个页面的顺序要和底部对应
  List<Widget> getPages (){
    List<Widget> list = [];
    list.add(const HomeView());
    if(controller.listSize == 4){
      list.add(const ProductView());
    }
    list.add(const MsgView());
    list.add(const MineView());
    return list;
  }

  // 点击返回键
  Future<bool> clickBack() async {
    final currentTime = DateTime.now();
    int timestamp = currentTime.millisecondsSinceEpoch;
    if(controller.lastBackPressTime == -1 || timestamp - controller.lastBackPressTime >= 2000){
      EasyLoading.showToast('再按一次退出');
      controller.lastBackPressTime = timestamp;
      return false;
    }else{
      return true;
    }
  }
}
